home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Help - sorting strings
- Date: Mon, 01 Apr 96 13:13:27 GMT
- Organization: none
- Message-ID: <828364407snz@genesis.demon.co.uk>
- References: <4j11ne$kj9$1@mhafn.production.compuserve.com> <4j67ki$hvr@transformer.pti-us.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4j67ki$hvr@transformer.pti-us.com>
- wv.dixon@pti-us.com "Walt Dixon" writes:
-
- >#include <stdlib.h>
- >
- >static int alphabetize( const void* arg1, const void* arg2 )
- >{
- > return strcmp(*(char**)arg1, *(char**)arg2));
- >}
-
- I suggest you write that as:
-
- return strcmp(*(char*const*)arg1, *(char*const*)arg2));
-
- While you can cast const away don't do it unless you have to. If nothing else
- in this case some compilers can warn about asting qualifiers away so the
- version with const has more chance of compiling cleanly.
-
- >
- >you pass a pointer to your sorting function to qsort. qsort will then
- >envoke your function as required to sort the array of strings.
- >Here is how you would envoke the routine.
- >
- >qsort(words,num_elements,sizeof(char*),alphabetize);
- >
- >words is declared to be a char**, num_elements is the number of strings,
- >sizeof(char*) is the size of each element in the char* array, and alphabetize
- >is the pointer to the comparison funciton.
- >
- >To reverse sort all you would have to do is to reverse the two arguments
- >to the strcmp function.
- >
- >I have attached a routine that reads in a bunch of strings. One string
- >per line, sorts then and prints them out. I did not clean up
- >the memory that was allocated. It is only an example.
- >
- >walt
- >
- >---------------------------------240801314710921
- >Content-Transfer-Encoding: 7bit
- >Content-Type: text/plain
- >
- >#include <stdlib.h>
- >#include <stdio.h>
- >#include <string.h>
- >
- >int compare(const void* a, const void* b)
- >{
- > return(strcmp(*(char**)b,*(char**)a));
- >}
-
- I suggest you write that as:
-
- return(strcmp(*(char*const*)b,*(char*const*)a));
-
- While you can cast const away don't do it unless you have to. If nothing else
- in this case some compilers can warn about asting qualifiers away so the
- version with const has more chance of compiling cleanly.
-
- >void main(int argc, char** argv)
- ^
- int
-
- >{
- > FILE* fh;
- > char** words;
- > char buffer[132];
- > int i,ii;
- >
- > if(argc != 2)
- > {
- > printf("No file specified.\n");
- > exit(1);
-
- 1 as an exit status has no defined meaning - use EXIT_FAILURE to indicate
- a failure.
-
- > }
- > fh = fopen(argv[1],"r");
- > if(fh)
- > {
- > words = (char**) malloc(500 * sizeof(char*));
- > i = 0;
- > while(fscanf(fh,"%s \n",buffer)==1)
- > {
- > words[i++] = strdup(buffer);
-
- strdup() isn't a standard library function - this simply won't compile/link
- on many systems.
-
- > }
- > printf("read %d words\n",i);
- > qsort(words,i,sizeof(char*),compare);
- > for(ii=0; ii < i; ii++)
- > {
- > printf("%s\n",words[ii]);
- > }
- > fclose(fh);
- > }
- > else
- > {
- > printf("Unable to open %s\n",argv[1]);
- > }
-
- return 0; /* Or return EXIT_SUCCESS */
- >}
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-